<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class FinanceController extends AdminController{
	/**
	 * 查询某个人的财务日志
	 */    
	public function getFinanceByMemberId(){
		$order = 'f.add_time desc';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'f.add_time':$order = 'f.add_time desc';
			 
		}
		$begin = I('begin');
		$last = I('last');
		$where = [];
		$member_id = I('member_id');
		$money_type = I('money_type');
		if(!empty($money_type)){
			$where['f.money_type'] = $money_type;
		}
		$type = I('type');
		if(!empty($type)){
			$where['f.type'] = $type;
		}
		$currency_id = I('currency_id');
		if(!empty($currency_id)||$currency_id == '0'){
			$where['f.currency_id'] = $currency_id;
		}
		if(empty($member_id)){
			$data = [
				'status'=>-1,
				'msg'=>'参数错误，缺少用户id'
			];
			$this->ajaxReturn($data);
		}
		$where['f.member_id'] = $member_id;
		$whereTime = $this->dotime($begin, $last);
		$where['f.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$page = I('page');
		$num = I('num');
		$pages = $this->DoPage($page, $num);
		$finance_type = M('Finance_type')->select();
		$table = $this->getMemberGroup(intval($member_id));
		$r = M('Finance_'.$table)
				->alias('f')
				->field('f.*,m.username,ft.name')
				->join('__MEMBER__ AS m on m.member_id = f.member_id','left')
				->join('__FINANCE_TYPE__ AS ft on ft.id = f.type','left')
				->where($where)
				->limit($pages['limit'],$pages['num'])
				->order($order)
				->select();
		$count = M('Finance_'.$table)
				->alias('f')
				->field('f.*,m.username,ft.name')
				->join('__MEMBER__ AS m on m.member_id = f.member_id','left')
				->join('__FINANCE_TYPE__ AS ft on ft.id = f.type','left')
				->where($where)
				->count();
		$data = [
			'status'=>1,
			'data'=>$r,
			'count'=>$count,
			'finance_type'=>$finance_type,
		];
		$this->ajaxReturn($data);
				
	}
	
	/**
	 * 查询所有的财务类型
	 */
	public function getAllType(){
		$type = M('Finance_type')->field('id,name')->select();
		$this->ajaxReturn($type);
	}
	/**
	 * 查询所有人的财务日志
	 */
	public function getFinanceAll(){
		$order = 'f.add_time desc';
		$sort = I('sort');
		if(!empty($sort)){
			if(!in_array($sort, [1,2])){
				$data = [
				'status'=>-1,
				'msg'=>'参数错误，排序参数错误'
						];
				$this->ajaxReturn($data);
			}
			$sort==1?$order = 'f.add_time':$order = 'f.add_time desc';
		}
		$username = I('username');
		$begin = I('begin');
		$last = I('last');
		$where = [];
		$member_id = I('member_id');
		$money_type = I('money_type');
		if(!empty($money_type)){
			$where['f.money_type'] = $money_type;
		}
		$type = I('type');
		if(!empty($type)){
			$where['f.type'] = $type;
		}
		if(!empty($username)){
			$where['m.username'] = ['like','%'.$username.'%'];
		}
		$currency_id = I('currency_id');
		if(!empty($currency_id)||$currency_id == '0'){
			$where['f.currency_id'] = $currency_id;
		}
		if(!empty($member_id)){
			$where['f.member_id'] = $member_id;
		}
		$whereTime = $this->dotime($begin, $last);
		$where['f.add_time'] = array('between',[$whereTime['begin'],$whereTime['last']]);
		$page = I('page');
		$num = I('num');
		$pages = $this->DoPage($page, $num);
		$finance_type = M('Finance_type')->select();
		$table = $this->getMemberGroup(intval($member_id));
		$r = M('Finance_view')
		->alias('f')
		->field('f.*,m.username,ft.name')
		->join('__MEMBER__ AS m on m.member_id = f.member_id','left')
		->join('__FINANCE_TYPE__ AS ft on ft.id = f.type','left')
		->where($where)
		->limit($pages['limit'],$pages['num'])
		->order($order)
		->select();
		$count = M('Finance_view')
		->alias('f')
		->field('f.*,m.username,ft.name')
		->join('__MEMBER__ AS m on m.member_id = f.member_id','left')
		->join('__FINANCE_TYPE__ AS ft on ft.id = f.type','left')
		->where($where)
		->count();
		$data = [
		'status'=>1,
		'data'=>$r,
		'count'=>$count,
		'finance_type'=>$finance_type,
		];
		$this->ajaxReturn($data);
	}
}